
import datetime

from db_interface import db_interface




def cal_stock_exchange_date(days=30):
    max_date = datetime.datetime.now()
    def get_max_date():
        sql = 'select max(date) from dongcai_ids_data'
        date = ''
        query = db_interface.stock_base.select(sql)
        for item in query:
            if item[0]:
                date = item[0]
        return date
    date_get = get_max_date()
    if date_get:
        max_date = date_get
    date_bef = max_date - datetime.timedelta(days=days)
    date_str = date_bef.strftime('%Y-%m-%d')
    sql = "select max(date) from dongcai_ids_data where date <= '{}' " \
          "union " \
          "select max(date) from dongcai_ids_data".format(date_str)
    query = db_interface.stock_base.select(sql)
    date_interval = []
    for item in query:
        date = item[0].strftime('%Y-%m-%d')
        date_interval.append(date)
    return date_interval



if __name__ == "__main__":
    r = cal_stock_exchange_date()
    print(r)


